<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

require_once(dirname(__FILE__) . '/lib_erp_common.php');


/*
* 获得产品属性，返回：
Array
(
    [211] => Array
        (
            [0] => Array
                (
                    [attr_name] => 颜色
                    [goods_attr_id] => 1
                    [attr_value] => 白色
                )

            [1] => Array
                (
                    [attr_name] => 颜色
                    [goods_attr_id] => 9
                    [attr_value] => 黑色
                )

        )

    [212] => Array
        (
            [0] => Array
                (
                    [attr_name] => 尺码
                    [goods_attr_id] => 7
                    [attr_value] => 38
                )

            [1] => Array
                (
                    [attr_name] => 尺码
                    [goods_attr_id] => 8
                    [attr_value] => 39
                )

            [2] => Array
                (
                    [attr_name] => 尺码
                    [goods_attr_id] => 10
                    [attr_value] => 40
                )

        )

)


*/
function get_goods_attr($goods_id)
{
	$sql="select ga.goods_attr_id,ga.attr_id,ga.attr_value,a.attr_name,a.attr_id from ".$GLOBALS['ecs']->table('goods_attr')." as ga ";
	$sql.=" left join ".$GLOBALS['ecs']->table('attribute')." as a ";
	$sql.=" on ga.attr_id=a.attr_id and a.attr_input_type='1' and a.attr_type='1' ";
	$sql.=" where ga.goods_id='".$goods_id."'";
	
	 
	//$sql="select ga.goods_attr_id,ga.attr_id,ga.attr_value from ".$GLOBALS['ecs']->table('goods_attr')." as ga, ";
	//$sql.=$GLOBALS['ecs']->table('attribute')." as a ";
	//$sql.=" where ga.goods_id='".$goods_id."' and ga.attr_id=a.attr_id and a.attr_input_type='1' and a.attr_type='1'";
		
	
	$goods_attrs=$GLOBALS['db']->getAll($sql);
	
	$result=array();
	
	if(!empty($goods_attrs))
	{
		foreach($goods_attrs as $key => $attr)
		{
			$tmp['attr_name']=$attr['attr_name'];
			
			$tmp['goods_attr_id']=$attr['goods_attr_id'];
			$tmp['attr_value']=$attr['attr_value'];
			
			$result[$attr['attr_id']][]=$tmp;
		}
	}
	
	return $result;
}

/*
* 更新ecs_erp_goods_attr表，添加产品属性
*/
function update_attr($goods_id)
{
	$goods_attrs=get_goods_attr($goods_id);
	
	$goods_attr_list=array();
	
	if(!empty($goods_attrs))
	{
		foreach($goods_attrs as $key => $item)
		{
			$tmp=array();
			if(!empty($item))
			{
				foreach($item as $k => $i)
				{
					$tmp[]=$i['goods_attr_id'];
				}
			}
			$goods_attr_list[]=$tmp;
		}
	}
		
	$goods_attr_list=get_array_combination($goods_attr_list);

	if(!empty($goods_attr_list))
	{
		foreach($goods_attr_list as $key => $item)
		{
			if(!is_attr_exists($goods_id,$item))//不存在
			{
				insert_attr_item($goods_id,$item);
			}
		}
		$sql="select attr_id,goods_qty from ".$GLOBALS['ecs']->table('erp_goods_attr')." where goods_id='".$goods_id."' and goods_attr_id_list='$$'";
		$attr_info=$GLOBALS['db']->getRow($sql);
		
		$goods_qty=$attr_info['goods_qty'];
		$attr_id=$attr_info['attr_id'];
		if($goods_qty==0)	
		{
			$sql="delete from ".$GLOBALS['ecs']->table('erp_goods_attr')." where attr_id='".$attr_id."'";
			$GLOBALS['db']->query($sql);
		}
	}
	else{
		$goods_attr=get_goods_attr($goods_id);
		if(empty($goods_attr))
		{
			$sql="select attr_id from ".$GLOBALS['ecs']->table('erp_goods_attr')." where goods_id='".$goods_id."' and goods_attr_id_list='$$'";
			$attr_id=$GLOBALS['db']->getOne($sql);
			if(empty($attr_id))
			{
				$sql="insert into ".$GLOBALS['ecs']->table('erp_goods_attr')." set goods_attr_id_list='$$', goods_id='".$goods_id."'";
				$GLOBALS['db']->query($sql);
			}
		}	
	}
}

function has_attr($goods_id)
{
	$sql="select ";
}


function get_array_combination($array=array())
{
	$count_total=count($array);
	$t=1;
	if($count_total>=2)
	{
		$result=array();
		for($i=0;$i<=$count_total-1;$i++)
		{
			$count[$i]=count($array[$i]);
		}
		
		foreach($count as $c)
		{
			$t=$t*$c;
		}
		
		for($j=1;$j<=$t;$j++)
		{
			for($i=0;$i<=$count_total-1;$i++)
			{
				$pos=rand(0,$count[$i]-1);
				$item[$i]=$array[$i][$pos];
			}
			$tmp='';
			for($i=0;$i<=$count_total-1;$i++)
			{
				if($i!=$count_total-1)
				{
					$tmp.=$item[$i].',';
				}
				else{
					$tmp.=$item[$i];
				}
			}
			
			while(in_array($tmp,$result))
			{
				for($i=0;$i<=$count_total-1;$i++)
				{
					$pos=rand(0,$count[$i]-1);
					$item[$i]=$array[$i][$pos];
				}
				$tmp='';
				for($i=0;$i<=$count_total-1;$i++)
				{
					if($i!=$count_total-1)
					{
						$tmp.=$item[$i].',';
					}
					else{
						$tmp.=$item[$i];
					}
				}			
			}
			
			array_push($result,$tmp);
		}
		
		return $result;
		
	}
	else{
		return $array[0];
	}
}

function delete_attr($goods_id,$goods_attr_id)
{
	$sql="select * from ".$GLOBALS['ecs']->table('erp_goods_attr')." where goods_id='".$goods_id."'";
	$attr_infos=$GLOBALS['db']->getAll($sql);
	if(!attr_has_qty($goods_id,$goods_attr_id))//无库存，可以删除
	{
		if(!empty($attr_infos))
		{
			foreach($attr_infos as $key=> $item)
			{
				$attr_id=$item['attr_id'];
				$goods_attr_id_list=$item['goods_attr_id_list'];

				$goods_attr_id_array=explode(',',$goods_attr_id_list);
				
				if(in_array($goods_attr_id,$goods_attr_id_array))
				{
					$sql="delete from ".$GLOBALS['ecs']->table('erp_goods_attr')." where attr_id='".$attr_id."' limit 1";
					$GLOBALS['db']->query($sql);
				}
			}
		}		
	}

}


function attr_has_qty($goods_id,$goods_attr_id)
{
	$has_qty=false;
	$sql="select * from ".$GLOBALS['ecs']->table('erp_goods_attr')." where goods_id='".$goods_id."'";
	$attr_infos=$GLOBALS['db']->getAll($sql);
	
	if(!empty($attr_infos))
	{
		foreach($attr_infos as $key=> $item)
		{
			$goods_attr_id_list=$item['goods_attr_id_list'];
			$goods_qty=$item['goods_qty'];
			
			$goods_attr_id_array=explode(',',$goods_attr_id_list);
			
			if(in_array($goods_attr_id,$goods_attr_id_array) && $goods_qty>0)
			{
				$has_qty=true;
				break;
			}
		}
	}
	return $has_qty;
}

function is_attr_exists($goods_id,$goods_attr_id_list)
{
	$exists=false;
	
	$goods_attr_id_list=explode(',',$goods_attr_id_list);
	
	$sql="select goods_attr_id_list from ".$GLOBALS['ecs']->table('erp_goods_attr')." where goods_id='".$goods_id."'";
	
	$attrs=$GLOBALS['db']->getAll($sql);
	
	if(!empty($attrs))
	{
		foreach($attrs as $key => $item)
		{
			$attr_id_list=explode(',',$item['goods_attr_id_list']);
			
			$tmp1=array_diff($goods_attr_id_list,$attr_id_list);
			$tmp2=array_diff($attr_id_list,$goods_attr_id_list);
			
			if(empty($tmp1) && empty($tmp2))
			{
				$exists=true;
			}
		}
	}
	return $exists;
}

function insert_attr_item($goods_id,$goods_attr_id_list)
{
	$sql="insert into ".$GLOBALS['ecs']->table('erp_goods_attr')." set goods_id='".$goods_id."', goods_attr_id_list='".$goods_attr_id_list."',goods_qty=0";
	$GLOBALS['db']->query($sql);
}

function delete_attr_item($goods_id,$goods_attr_id_list)
{
	$goods_attr_id_list=explode(',',$goods_attr_id_list);
	
	$sql="select attr_id,goods_id,goods_qty,goods_attr_id_list from ".$GLOBALS['ecs']->table('erp_goods_attr')." where goods_id='".$goods_id."'";
	
	$attrs=$GLOBALS['db']->getAll($sql);
	
	if(!empty($attrs))
	{
		foreach($attrs as $key => $item)
		{
			$attr_id=$item['attr_id'];
			$attr_id_list=explode(',',$item['goods_attr_id_list']);
			
			$tmp1=array_diff($goods_attr_id_list,$attr_id_list);
			$tmp2=array_diff($attr_id_list,$goods_attr_id_list);
			
			if(empty($tmp1) && empty($tmp2))
			{
					$sql="delete from ".$GLOBALS['ecs']->table('erp_goods_attr')."  where  attr_id='".$attr_id."' limit 1";
					$GLOBALS['db']->query($sql);
					break;
			}
		}
	}		
}


function set_attr_qty($goods_id,$goods_attr_id_list,$goods_qty,$act='update')
{
	$goods_attr_id_list=explode(',',$goods_attr_id_list);
	
	$sql="select attr_id,goods_id,goods_qty,goods_attr_id_list from ".$GLOBALS['ecs']->table('erp_goods_attr')." where goods_id='".$goods_id."'";
	
	$attrs=$GLOBALS['db']->getAll($sql);
	
	if(!empty($attrs))
	{
		foreach($attrs as $key => $item)
		{
			$attr_id=$item['attr_id'];
			$attr_id_list=explode(',',$item['goods_attr_id_list']);
			
			$tmp1=array_diff($goods_attr_id_list,$attr_id_list);
			$tmp2=array_diff($attr_id_list,$goods_attr_id_list);
			
			if(empty($tmp1) && empty($tmp2))
			{
					if($act=='update')
					{
						$sql="update ".$GLOBALS['ecs']->table('erp_goods_attr')." set goods_qty='".$goods_qty."' where  attr_id='".$attr_id."'";
					}
					elseif($act=='add')
					{
						$sql="update ".$GLOBALS['ecs']->table('erp_goods_attr')." set goods_qty=goods_qty+'".$goods_qty."' where  attr_id='".$attr_id."'";
						}
					
					$GLOBALS['db']->query($sql);
					break;
			}
		}
	}	
}


function get_attr_id($goods_id,$goods_attr_id_list)
{
	$attr_id=0;
	
	$goods_attr_id_list=explode(',',$goods_attr_id_list);
	
	$sql="select attr_id,goods_attr_id from ".$GLOBALS['ecs']->table('goods_attr')." where goods_id='".$goods_id."'";
	
	$attrs=$GLOBALS['db']->getAll($sql);
	
	if(!empty($attrs))
	{
		foreach($attrs as $key => $item)
		{
			$attr_id=$item['attr_id'];
			$attr_id_list=explode(',',$item['goods_attr_id']);
			
			$tmp1=array_diff($goods_attr_id_list,$attr_id_list);
			$tmp2=array_diff($attr_id_list,$goods_attr_id_list);
			
			if(empty($tmp1) && empty($tmp2))
			{
				return $attr_id;
			}
		}
	}	
}

/*
*获得产品属性库存数
*/
function get_attr_qty($goods_id,$goods_attr_id_list)
{
	$goods_qty=0;
	
	$goods_attr_id_list=explode(',',$goods_attr_id_list);
	
	$sql="select goods_qty,goods_attr_id_list from ".$GLOBALS['ecs']->table('erp_goods_attr')." where goods_id='".$goods_id."'";
	
	$attrs=$GLOBALS['db']->getAll($sql);
	
	if(!empty($attrs))
	{
		foreach($attrs as $key => $item)
		{
			$goods_qty=$item['goods_qty'];
			$attr_id_list=explode(',',$item['goods_attr_id_list']);
			
			$tmp1=array_diff($goods_attr_id_list,$attr_id_list);
			$tmp2=array_diff($attr_id_list,$goods_attr_id_list);
			
			if(empty($tmp1) && empty($tmp2))
			{
				return $goods_qty;
			}
		}
	}	
}

/*
*从ecs_erp_goods_attr表里获得产品属性信息
返回：
Array
(
    [attr_info] => Array
        (
            [0] => Array
                (
                    [attr_name] => 颜色
                    [attr_value] => 黑色
                )

            [1] => Array
                (
                    [attr_name] => 尺码
                    [attr_value] => 40
                )

        )

    [goods_id] => 33
    [goods_qty] => 0
)


*/
function get_attr_info($attr_id)
{
	$sql="select goods_id,goods_attr_id from ".$GLOBALS['ecs']->table('goods_attr')." where attr_id='".$attr_id."'";
	$attr_info=$GLOBALS['db']->getRow($sql);
	
	if(!empty($attr_info))
	{
		$goods_attr_id_list=explode(',',$attr_info['goods_attr_id']);
		foreach($goods_attr_id_list as $key => $item)
		{
			$goods_attr_id=$item;
			
			$sql="select a.attr_name,ga.attr_value from ".$GLOBALS['ecs']->table('goods_attr')." as ga,";
			$sql.=$GLOBALS['ecs']->table('attribute')." as a where ";
			$sql.="ga.goods_attr_id='".$goods_attr_id."' and ga.attr_id=a.attr_id";
			
			$info=$GLOBALS['db']->getRow($sql);
			
			$result['attr_info'][]=$info;		
		}
		
		$result['goods_id']=$attr_info['goods_id'];
		$result['goods_qty']=1;
		
		return $result;
	}
	else{
		return array();
	}
}

?>